package com.ehotting.edsta.circuitDesign.gen.circuit.script;

import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.gen.element.enums.ScriptTypeEnum;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AssignmentCircuitScript extends CircuitScriptAbstract {

    @Override
    public String invoke(EsRuleScript esScript, List<String> argumentsList, String functionName) {
        StringBuffer buf = new StringBuffer();
        buf.append(functionStart( functionName, argumentsList));
        buf.append(execute( esScript));
        buf.append(functionEnd());
        return buf.toString();
    }

    protected String execute(EsRuleScript esScript){
        StringBuffer buf = new StringBuffer();
        String[] lines = esScript.getContent().split(ScriptConstants.LINE);
        for(String line : lines){
            String[] ss = line.split("=");
            buf.append("def ").append(line).append(line.endsWith(";")?"":";").append(ScriptConstants.LINE);
            buf.append("context.put(\""+ss[0]+"\","+ss[0]+");").append(ScriptConstants.LINE);
        }
        return buf.toString();
    }

    @Override
    public boolean isTrue(ScriptTypeEnum code){
        return ScriptTypeEnum.ASSIGNMENT == code;
    }

    @Override
    public String getExample() {
        StringBuffer buf = new StringBuffer();
        buf.append("输入格式:");
        buf.append("r1=200").append(ScriptConstants.LINE);
        buf.append("或者多行，回车隔开:");
        buf.append("r1=200").append(ScriptConstants.LINE);
        buf.append("r2=400").append(ScriptConstants.LINE);
        buf.append("或者范围值，分号隔开:");
        buf.append("r1Max=200;r1Min=300").append(ScriptConstants.LINE);
        buf.append("r2Max=400;r2Min=400").append(ScriptConstants.LINE);
        return buf.toString();
    }
}
